﻿using System;
using System.Net;
using System.Xml.Linq; 
using TestApp.Model;
using System.Collections.Generic;

namespace TestApp.WebService.Parsers
{
    public class CouponsParser : Parser
    {
        // Collection of coupons
        private static String COUPONS_COLLECTION_ELEMENT = @"CollectionOfCoupons";
        // Constant for the Coupons element
        private static String COUPONS_ELEMENT = @"Coupons";
        // Constant for the Coupon element
        private static  String COUPON_ELEMENT = @"Coupon";
        // Constant for the Id element
        private static  String ID_ELEMENT = @"Id";
        // Constant for the Name element
        private static  String NAME_ELEMENT = @"Name";
        // Constant for the Value element
        private static  String VALUE_ELEMENT = @"Value";
        // Constant for the Summary element
        private static  String SUMMARY_ELEMENT = @"Summary";
        // Constant for the Description element
        private static  String DESCRIPTION_ELEMENT = @"Description";
        // Constant for the Vendor element
        private static  String VENDOR_ELEMENT = @"Vendor";
        // Constant for the Expiration Date element
        private static  String EXPIRATION_DATE_ELEMENT = @"ExpirationDate";
        // Constant for the ImageUrl element
        private static  String IMAGE_URL_ELEMENT = @"Image";
        // Constant for the Brand element
        private static  String BRAND_ELEMENT = @"Brand";
        // Constant for the State element
        private static  String STATE_ELEMENT = @"State";
        // Constant for the IsRemovable element
        private static  String IS_REMOVABLE_ELEMENT = @"IsRemovable";
        // Constant for the Category element
        private static  String CATEGORY_ELEMENT = @"Category";
        // Constant for the Count element
        private static  String COUNT_ELEMENT = @"Count";
        // Constant for the Target element
        private static  String TARGET_ELEMENT = @"Target";


        public override object ParseResults(WSResponse results)
        {
            XDocument xmlDocument = XDocument.Parse(results.Data);
            String nameSpace = xmlDocument.Root.Name.Namespace.NamespaceName;

            IEnumerable<XElement> xmlCoupons = xmlDocument.Root.Element(XName.Get(COUPONS_ELEMENT,nameSpace)).Elements();
            List<Coupon> couponsList = new List<Coupon>();
            foreach (XElement xmlCoupon in xmlCoupons) {
                Coupon coupon = new Coupon();
                // Coupon Id
                coupon.Id = xmlCoupon.Element(XName.Get(ID_ELEMENT, nameSpace)).Value;
                // Coupon Name
                coupon.Name = xmlCoupon.Element(XName.Get(NAME_ELEMENT, nameSpace)).Value;
                // Coupon Value
                coupon.Value = xmlCoupon.Element(XName.Get(VALUE_ELEMENT, nameSpace)).Value;
                // Coupon Summary
                coupon.Summary = xmlCoupon.Element(XName.Get(SUMMARY_ELEMENT, nameSpace)).Value;
                // Coupon Description
                coupon.Description = xmlCoupon.Element(XName.Get(DESCRIPTION_ELEMENT, nameSpace)).Value;
                // Coupon Vendor
                coupon.Vendor = xmlCoupon.Element(XName.Get(VENDOR_ELEMENT, nameSpace)).Value;
                // Coupon Image Url
                coupon.ImageUrl = xmlCoupon.Element(XName.Get(IMAGE_URL_ELEMENT, nameSpace)).Value;
                // Coupon Brand
                coupon.Brand = xmlCoupon.Element(XName.Get(BRAND_ELEMENT, nameSpace)).Value;
                // Coupon State
                coupon.State = xmlCoupon.Element(XName.Get(STATE_ELEMENT, nameSpace)).Value;
                // Coupon IsRemovable
                coupon.IsRemovable = bool.Parse(xmlCoupon.Element(XName.Get(IS_REMOVABLE_ELEMENT, nameSpace)).Value);
                // Coupon Target
                coupon.Target = xmlCoupon.Element(XName.Get(TARGET_ELEMENT, nameSpace)).Value;
                // Coupon Expiration Date
                coupon.ExpirationDate = DateTime.Parse(xmlCoupon.Element(XName.Get(EXPIRATION_DATE_ELEMENT,nameSpace)).Value);
                // Coupon Category
                XElement xmlCategory = xmlCoupon.Element(XName.Get(CATEGORY_ELEMENT, nameSpace));
                coupon.Category = new CouponCategory();
                coupon.Category.CategoryId = xmlCategory.Element(XName.Get(ID_ELEMENT, nameSpace)).Value;
                coupon.Category.Name = xmlCategory.Element(XName.Get(NAME_ELEMENT, nameSpace)).Value;
                // Add Coupon to list
                couponsList.Add(coupon); 
            }
            return couponsList;
        }
    }
}
